import http from '@/utils/http-util'
import axios from 'axios'

let BASE_IP_CONTENT = process.env.WEB_CONTEXT_PATH + 'ip'
/** 获取列表 **/
export function getIPListApi(data = {}) {
  return http({
    url: BASE_IP_CONTENT + '/list',
    method: 'post',
    data: http.bodyData({
      'page': data.page || 1,
      'pageSize': data.pageSize || 20,
      'sort': {
        'field': data.sortField,
        'rule': data.sortRule === undefined ? 1 : data.sortRule
      },
      'minCreateTime': data.minCreateTime || '',
      'maxCreateTime': data.maxCreateTime || '',
      'ip': data.ip || ''
    })
  })
}

/** 删除 **/
export function deleteIPApi(id = 0) {
  return http({
    url: BASE_IP_CONTENT + '/' + id,
    method: 'delete',
    params: http.queryParam()
  })
}

/** 获取信息 **/
export function getIPInfoApi(id = 0) {
  return http({
    url: BASE_IP_CONTENT + '/' + id,
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 保存 **/
export function saveIPApi(data = {}) {
  return http({
    url: BASE_IP_CONTENT,
    method: 'post',
    data: http.bodyData({
      ip: data.ip,
      country: data.country,
      province: data.province,
      city: data.city,
      isp: data.isp,
      white: data.white,
      black: data.black,
      system: data.system,
      remark: data.remark
    }),
    loading: false
  })
}

/** 修改 **/
export function updateIPApi(data = {}) {
  if (!data.id || data.id === 0 || data.id === '0') return
  return http({
    url: BASE_IP_CONTENT + '/' + data.id,
    method: 'put',
    data: http.bodyData({
      ip: data.ip,
      country: data.country,
      province: data.province,
      city: data.city,
      isp: data.isp,
      white: data.white,
      black: data.black,
      system: data.system,
      remark: data.remark
    }),
    loading: false
  })
}

/** 获取IP解析信息 **/
export function getIPAnalysisApi(ip = '') {
  return axios.get('https://ip.huomao.com/ip?ip=' + ip)
}
